package me.cjj.android.assignment.model;

import android.util.Pair;

public class QuadraticQuestion implements IQuestion {

    private int a;
    private int b;
    private int c;

    public QuadraticQuestion(int a, int b, int c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    @Override
    public int getAnswerCount() {
        return 2;
    }

    @Override
    public String getQuestionText() {
        String aText = Utils.toQuestionText(a, "x^2", true);
        String bText = Utils.toQuestionText(b, "x", false);
        String cText = Utils.toQuestionText(c, "", false);
        return "\\(" + aText + bText + cText + "=0\\)";
    }

    public boolean isValid() {
        return -4 * a * c + b * b > 0;
    }

    public boolean judge(float... answers) {
        Pair<Float, Float> answer = getAnswer();
        Float correct1 = answer.first;
        Float correct2 = answer.second;
        if ((answers[0] == correct1 && answers[1] == correct2) || (answers[1] == correct1 && answers[0] == correct2)) {
            return true;
        }
        return false;
    }

    private Pair<Float, Float> getAnswer() {
        double sqrt = Math.sqrt((double) (b * b - 4 * a * c));
        float correct1 = (float) (sqrt - b) / (float) (2 * a);
        float correct2 = (float) (-sqrt - b) / (float) (2 * a);
        correct1 = (float) (Math.round(correct1 * 100.0) / 100.0);
        correct2 = (float) (Math.round(correct2 * 100.0) / 100.0);
        return Pair.create(correct1, correct2);
    }

    public String getAnswerText() {
        Pair<Float, Float> answer = getAnswer();
        return answer.first + " , " + answer.second;
    }

}
